const http = (method = 'GET', url, data) => {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: url,
            type: method,
            headers: {'token': localStorage.getItem('token')},
            dataType: 'json',
            contentType: 'application/json',
            data: data ? JSON.stringify(data) : null,
            success: res => {
                if (res.code === 2000) {
                    resolve(res);
                } else {
                    reject(res);
                    alert(res.message)
                }
            },
            error: res => reject(res)
        });
    });
}
let util = {
    getFormData: selector => {
        const formData = {};
        $(selector).serialize().split('&').forEach(item => {
            const [key, value] = item.split('=');
            formData[key] = value;
        });
        return formData;
    },
    get: (url) => {
        return http('GET', url, null);
    },
    delete: (url) => {
        return http('DELETE', url, null);
    },
    post: (url, data) => {
        return http('POST', url, data);
    },
    put: (url, data) => {
        return http('PUT', url, data);
    },
    clear: () => {
        localStorage.clear();
    }
}
window.$util = util;